package com.gitee.wsl.time.chinese.bean.eightchar


import com.gitee.wsl.ext.base.length
import com.gitee.wsl.time.chinese.Lunar
import com.gitee.wsl.time.chinese.util.LunarUtil
import kotlin.collections.get
import kotlin.invoke

/**
 * 大运
 *
 * @author 6tail
 */
class DaYun(
    /**
     * 运
     */
    private val yun: Yun,
    /**
     * 序数，0-9
     */
    val index: Int
) {
    /**
     * 开始年(含)
     */
    val startYear: Int

    /**
     * 结束年(含)
     */
    val endYear: Int

    /**
     * 开始年龄(含)
     */
    val startAge: Int

    /**
     * 结束年龄(含)
     */
    val endAge: Int

    private val lunar: Lunar = yun.getLunar()

    init {
        val birthYear: Int = lunar.solar.year
        val year: Int = yun.startSolar.year
        if (index < 1) {
            this.startYear = birthYear
            this.startAge = 1
            this.endYear = year - 1
            this.endAge = year - birthYear
        } else {
            val add = (index - 1) * 10
            this.startYear = year + add
            this.startAge = this.startYear - birthYear + 1
            this.endYear = this.startYear + 9
            this.endAge = this.startAge + 9
        }
    }

    fun getLunar(): Lunar {
        return lunar
    }

    val ganZhi: String
        /**
         * 获取干支
         *
         * @return 干支
         */
        get() {
            if (index < 1) {
                return ""
            }
            var offset: Int = LunarUtil.getJiaZiIndex(lunar.monthInGanZhiExact)
            offset += if (yun.isForward) index else -index
            val size: Int = LunarUtil.JIA_ZI.length
            if (offset >= size) {
                offset -= size
            }
            if (offset < 0) {
                offset += size
            }
            return LunarUtil.JIA_ZI[offset]
        }

    val xun: String
        /**
         * 获取所在旬
         * @return 旬
         */
        get() = LunarUtil.getXun(this.ganZhi)

    val xunKong: String
        /**
         * 获取旬空(空亡)
         * @return 旬空(空亡)
         */
        get() = LunarUtil.getXunKong(this.ganZhi)

    val liuNian: Array<LiuNian>
        /**
         * 获取10轮流年
         *
         * @return 流年
         */
        get() = getLiuNian(10)

    /**
     * 获取流年
     *
     * @param n 轮数
     * @return 流年
     */
    fun getLiuNian(n: Int): Array<LiuNian> {
        var n = n
        if (index < 1) {
            n = endYear - startYear + 1
        }
        val l: Array<LiuNian> = Array<LiuNian>(n){i->
            LiuNian(this, i)
        }
//        for (i in 0..<n) {
//            l[i] = LiuNian(this, i)
//        }
        return l
    }

    val xiaoYun: Array<XiaoYun>
        /**
         * 获取10轮小运
         *
         * @return 小运
         */
        get() = getXiaoYun(10)

    /**
     * 获取小运
     * @param n 轮数
     * @return 小运
     */
    fun getXiaoYun(n: Int): Array<XiaoYun> {
        var n = n
        if (index < 1) {
            n = endYear - startYear + 1
        }
//        val l: Array<XiaoYun> = kotlin.arrayOfNulls<XiaoYun>(n)
//        for (i in 0..<n) {
//            l[i] = XiaoYun(this, i, yun.isForward())
//        }
//        return l
        return Array<XiaoYun>(n){i->
            XiaoYun(this, i, yun.isForward)
        }
    }
}
